<?php

namespace App\Http\Middleware;

use App\Tools\JwtTools;
use Closure;
use Illuminate\Http\Request;

class User
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure(\Illuminate\Http\Request): (\Illuminate\Http\Response|\Illuminate\Http\RedirectResponse)  $next
     * @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse
     */
    public function handle(Request $request, Closure $next)
    {
        $token = $request->header('token');
        if (!$token){
           return response()->json(['code'=>2009,'msg'=>"未发送token",'data'=>[]]);
        }
        try {
            $request->id = JwtTools::transitionId($token);
        }catch (\Exception $exception){
            return response()->json(['code'=>$exception->getCode(),'msg'=>$exception->getMessage(),'data'=>[]]);
        }
        return $next($request);
    }
}
